<?php
/*
	I realize now that the assignmet asked for the functionality to be contained on one page.  I didnt read that 
until I was done with the object part.  I have a much easier time working with php when I use objects, so I am sorry 
for not placing it all in one file.  I assumed you would be happy to see someone using OOP in php but I realize now that I have caused you hertburn over it.

Having said that, allow me to explain the files and what they do:
myBlog.php: is the index page for the blog site. This is where adding and listing blogs takes place.  you also delete blogs from here.

editBlog.php: Is for editing of existing blogs.

Blog.inc: is the object representing the blog.  All of the Data Access is done here.


*/




//go ahead and get the blog object from the filesystem
require('Blog.inc');
$blog = new Blog(null);
//create the connection to the DB
$dbh = new PDO('mysql:host=localhost;dbname=blog', 'root', '');
//Provide an error message for outputing status
$status = '';
if($_POST){
	$blog = new Blog($_POST);
	if(isset($blog->title, $blog->body)){
		$blog->add($dbh);
		$status = 'Your blog entered succesfully';
	}else{
		$status = 'You must enter a title and a body to add a blog.  Please try again.';
	}
}
if($_GET && $_GET['delete']==1){
	$blog = new Blog($_GET);
	$blog->delete($dbh);
}

//Thankfully a lot of the logic is on the object so we can jump right down to the view and start spitting stuff out.
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Bryant's Blog</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
		<script type="text/javascript" src="common.js"></script>
	</head>

	<body>
		<div id="container">

			<div id="header">
Welcome to Bryants Blog.  Don't miss anything cool, return often to see how it has not changed.<br />
	<?php echo "$status"; ?>
			</div>

			<div id="wrapper">
				<div id="main">
					<table>

					<?php
					$blogs = $blog->getAll($dbh);
					foreach($blogs as $row){
						$blog = new Blog($row);
					
						echo "<tr>";
						echo "<td><a href='editBlog.php?id=$blog->id'>edit</a></td><td>$blog->title</td><td><a href='myBlog.php?id=$blog->id&delete=1'>delete</a></td>";
						echo "</tr>";
					}
					?>
					</table>
					<form method="post" action="myBlog.php">
						Title:  
						<input type="text" name="title" id="title" size="45"/> <br />
						Blog:<br />
						<textarea name="body" id="body"></textarea><br />
						<input type="submit" name="submit" value="submit" />
					</form>
				</div>
			</div>

			<div id="footer">
Bryant Larsen copyright 2008
			</div>
			
		</div>
	</body>

</html>
